package com.tz.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * <b>类名称：</b>DateUtil <br/>
 * <b>类描述：</b>日期工具类<br/>
 * <b>创建人：</b>zhanghj<br/>
 * <b>修改人：</b><br/>
 * <b>修改时间：</b>2015年11月9日 下午8:25:30<br/>
 * <b>修改备注：</b><br/>
 * <b>版本信息：</b>v1.0.0<br/>
 */

public class DateUtil {
	public static final int H_M = 0;
	public static final int H_M_S = 1;
	public static final int Y_H_M = 2;
	public static final int Y_H_M_H_M = 3;
	public static final int Y_H_M_H_M_S = 4;
	public static final int Y=5;
	public static final int Y_H=6;
	public static final int Y_H_M_H_M_S_2=7;
	public static final int yyyy_MM_ddTHH_mm_ss_z=8;
	public static final int Y_M_S_SLASH = 9;
	public static final int Y_H_M_H_M_S_SLASH = 10;
	
	static class CalendarFactory { 
	    private ThreadLocal<Calendar> calendarRef = new ThreadLocal<Calendar>() { 
	      protected Calendar initialValue() { 
	        return new GregorianCalendar(); 
	      } 
	    }; 
	    private static CalendarFactory instance = new CalendarFactory(); 
	   
	    public static CalendarFactory getFactory() { return instance; } 
	   
	    public Calendar getCalendar() { 
	      return calendarRef.get(); 
	    } 
	   
	    // Don't let outsiders create new factories directly 
	    private CalendarFactory() {} 
	  } 
	public static SimpleDateFormat getFormat(int num){
		SimpleDateFormat dateformat = null;
		switch(num){
			case DateUtil.H_M:
				dateformat = new SimpleDateFormat("HH:mm");
				break;
			case DateUtil.H_M_S:
				dateformat = new SimpleDateFormat("HH:mm:ss");
				break;
			case DateUtil.Y_H_M:
				dateformat = new SimpleDateFormat("yyyy-MM-dd");
				break;
			case DateUtil.Y_H_M_H_M:
				dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				break;
			case DateUtil.Y_H_M_H_M_S:
				dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				break;
			case DateUtil.Y:
				dateformat = new SimpleDateFormat("yyyy年");
				break;
			case DateUtil.Y_H:
				dateformat = new SimpleDateFormat("yyyy年MM月");
				break;
			case DateUtil.Y_H_M_H_M_S_2:
				dateformat = new SimpleDateFormat("yyyyMMddHHmmss");
				break;
			case DateUtil.yyyy_MM_ddTHH_mm_ss_z:
				dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+08:00'");
				break;
			case DateUtil.Y_M_S_SLASH:
				dateformat = new SimpleDateFormat("yyyy/MM/dd");
				break;
			case DateUtil.Y_H_M_H_M_S_SLASH:
				dateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				break;
			default:break;
		}
		return dateformat;
	}
	/**
	 * @description 返回格式化字符串型日期
	 * @param num 类型
	 * @param date 需要处理的日期
	 * @return 返回需要的日期字符串形式
	 */
	public static String getFormatString(int num, Date date){
		if(date == null) 
			return "";
		else{
			SimpleDateFormat dateformat = getFormat(num);
			return dateformat==null?"":dateformat.format(date);
		}
	}
	
	/**
	 * @description 返回格日期
	 * @param num 格式化类型标识
	 * @param stringDate 日期的字符串形式
	 * @return Date对象
	 */
	public static Date getFormatDate(int num, String stringDate){
		Date resultDate = null;
		SimpleDateFormat dateformat = getFormat(num);
		try {
			 if(dateformat!=null)
				 resultDate = dateformat.parse(stringDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return resultDate;
	}
	/**
	 * 
	 * @Title getNextDate
	 * @Description 返回指定日期的下一天
	 * @param currentDate
	 * @return 
	 * Date 
	 * @author zhanghj
	 * @date 2015年12月3日-上午8:49:03
	 * @update 
	 * @throws
	 */
	public static Date getNextDate(Date currentDate){
		return getAddDate(currentDate,1,1);
	}
	/**
	 * 
	 * @Title getNextDate
	 * @Description 获取下一天的日期
	 * @param stringDate
	 * @return 
	 * Date 
	 * @author zhanghj
	 * @date 2016年7月7日-上午11:13:38
	 * @update 
	 * @throws
	 */
	public static String getNextDate(String stringDate){
		return getFormatString(2,getNextDate(getFormatDate(DateUtil.Y_H_M,stringDate)));
	}
	/**
	 * 
	 * @Title getAddDate
	 * @Description 给指定日期添加时间
	 * @param date 需要操作日期
	 * @param type 添加类型
	 * @param num  具体时间值
	 * @return 
	 * Date 
	 * @author zhanghj
	 * @date 2015年12月3日-上午8:46:54
	 * @update 
	 * @throws
	 */
	public static Date getAddDate(Date date,int type,int num){
		Calendar cal = CalendarFactory.getFactory().getCalendar();
		try {
			cal.setTime(date);
			if(type==1){
				cal.add(Calendar.DATE, num);
			}else if(type==2){
				cal.add(Calendar.HOUR, num);
			}else if(type==3){
				cal.add(Calendar.MINUTE, num);
			}else if(type==4){
				cal.add(Calendar.SECOND, num);
			}else if(type==5){
				cal.add(Calendar.MONTH, num);
			}else if(type==6){
                cal.add(Calendar.YEAR, num);
            }
		} catch (Exception e) {
			//TODO 
		}
		return cal.getTime();
	}
	public static String formate(Date date,String pattern){
		if(null == date){
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern); 
		return sdf.format(date);
	}
	
	public static long intervalDays(String date1,String date2){
		long daysBetween=0;
		try{
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Date d1=sdf.parse(date1);
			Date d2=sdf.parse(date2);
			daysBetween=(d2.getTime()-d1.getTime()+1000000)/(3600*24*1000);
		}catch(ParseException e){
			e.printStackTrace();
		}
		  return daysBetween;
	}
	/**
	*字符串的日期格式的计算
	*/
	public static int daysBetween(String smdate,String bdate){
		long between_days=0;
		try{
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(smdate));
			long time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
			long time2 = cal.getTimeInMillis();
			between_days=(time2-time1)/(1000*3600*24);
		}catch(ParseException e){
			e.printStackTrace();
		}
	
		return Integer.parseInt(String.valueOf(between_days));
	} 
	
	/**
	* 计算两个日期之间相差的天数
	* @param smdate 较小的时间
	* @param bdate 较大的时间
	* @return 相差天数
	* @throws ParseException
	*/
	public static int daysBetween(Date smdate,Date bdate) throws ParseException
	{
		long between_days =0;
		try{
			
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			smdate=sdf.parse(sdf.format(smdate));
			bdate=sdf.parse(sdf.format(bdate));
			Calendar cal = Calendar.getInstance();
			cal.setTime(smdate);
			long time1 = cal.getTimeInMillis();
			cal.setTime(bdate);
			long time2 = cal.getTimeInMillis();
			between_days=(time2-time1)/(1000*3600*24);
		}catch(ParseException e){
			e.printStackTrace();
		}
	
		return Integer.parseInt(String.valueOf(between_days));
	} 
	public static void main(String[] args) {
		Date now =new Date();
		System.out.println(getFormatString(Y_H_M, getNextDate(now)));
		now.setHours(0);
		now.setMinutes(0);
		now.setSeconds(0);
		String hms=getFormatString(DateUtil.H_M_S, now);
		System.out.println("00:00:00".equals(hms));
	}

}
